package de.ur.iw.wiki;

import java.util.ArrayList;

public class Paragraph implements LinkCollector {
	
	private String name;
	private ArrayList<Link> links = new ArrayList<Link>();
	private ArrayList<Paragraph> subparagraphs = new ArrayList<Paragraph>();
	
	private WikiPage context;
	
	public Paragraph(String name, WikiPage context){
		this.name = name.trim();
		this.context = context;
	}
	
	public WikiPage getContext(){
		return context;
	}
	
	public ArrayList<Link> getAllLinks(){
		ArrayList<Link> result = new ArrayList<Link>();
		result.addAll(links);
		for(int i=0; i<subparagraphs.size(); i++){
			result.addAll(subparagraphs.get(i).getAllLinks());
		}
		return result;
	}
	
	public void add(Link l){
		links.add(l);
	}
	
	public void addAll(ArrayList<Link> links){
		this.links.addAll(links);
	}
	
	public void setName(String name){
		this.name = name.trim();
	}
	
	public String getName(){
		return name;
	}
	
	public String toString(){
		return name;
	}

	@Override
	public LinkCollector getParagraphAt(int i) {
		if(i<0 || i>=subparagraphs.size()){
			return null;
		}
		return subparagraphs.get(i);
	}
	
	public int getParagraphCount(){
		return subparagraphs.size();
	}

	@Override
	public void addParagraph(LinkCollector subparagraph) {
		if(subparagraph instanceof Paragraph){
			subparagraphs.add((Paragraph)subparagraph);
		}
	}
	
}
